#include <QCoreApplication>
#include <QDebug>
#include <QSocketNotifier>
#include <QTextStream>
#include <QDir>

#include <stdio.h>
#include <stdlib.h>

#include <unistd.h> //Provides STDIN_FILENO
/// log file handler here postfix incomming pipe handle...
#include "qlogger.h" // from https://github.com/francescmm/qt-coding/tree/master/QLogger 

#define __LOG_DIR__ \
             QString("%1/pipelog/").arg(QDir::homePath())
#define __LOG_File__ \
             QString("%1postfixlog.txt").arg(__LOG_DIR__)




#if 0 //// 1 or 0 to disable qDebug = PEEPME
#define PEEPME qDebug
#else
#define PEEPME if (0) qDebug
#endif

void MessageRewrite(QtMsgType type, const char *msg) {
    const QString modrewriteMSG = QString::fromUtf8(msg); /// i hope is utf-8
    //DebugLevel, InfoLevel, WarnLevel, ErrorLevel, FatalLevel 
    switch (type) {
        case QtDebugMsg:
            QLogger::QLog_("PipeApp", QLogger::InfoLevel, modrewriteMSG);
            break;
        case QtWarningMsg:
            QLogger::QLog_("PipeApp", QLogger::InfoLevel, modrewriteMSG);
            break;
        case QtCriticalMsg:
            QLogger::QLog_("PipeApp", QLogger::InfoLevel, modrewriteMSG);
            break;
        case QtFatalMsg:
            QLogger::QLog_("PipeApp", QLogger::InfoLevel, modrewriteMSG);
            abort();
    }
}
//// switch to qt5  
#define MEMOVERSIONCONFIG \
              QString("/Users/pro/Qt5-Source/current/bin/") 
#define __APPVERSION__ \
              QString("Ve.0.1.0")
#define __APPNAME__ \
              QString("DComunicator")
const int pointo = 76;
/* catch event from QCoreApplication if haveit more */
#define osApplication \
    (static_cast<PipeApp*>(QCoreApplication::instance()))

class PipeApp : public QCoreApplication {

    Q_OBJECT

public:
    PipeApp(int &argc, char **argv)
    : QCoreApplication(argc, argv) {
    
         QDir dir;
         if (!dir.exists(__LOG_DIR__)) {
            dir.mkpath(__LOG_DIR__);
         }
    
        this->setApplicationName(__APPNAME__);
        QTextStream out(stdout); /// stdin 
        QString str("*");
        setOrganizationName("Pipe STREAM");
        setOrganizationDomain("Postfix-Drupal-Handshake");
        manager = QLogger::QLoggerManager::getInstance();
        manager->addDestination(__LOG_File__, __APPNAME__, QLogger::InfoLevel);
        notifier = new QSocketNotifier(STDIN_FILENO, QSocketNotifier::Read);
        connect(notifier, SIGNAL(activated(int)), this, SLOT(handleMail()));
        out <<  "### Hi dear Postfix MX write your chunk please,\n i need from mail & remote host ok? i am qt version -> " << QT_VERSION;
        out << "\n";
        out.flush();
    };

signals:
    void textReceived(QString message);
public slots:

    void handleMail() {
        QTextStream qin(stdin);
        QString line = qin.readLine();
        QLogger::QLog_(__APPNAME__, QLogger::InfoLevel,line);
        PEEPME() << "#incomming postfix chat:\n" << line;
        // later emit textReceived(line);
    };

protected:

    bool event(QEvent *ev) {
        const int weather = ev->type();
        PEEPME() << "### run event  -> " << weather;
        bool cursor;
        cursor = QCoreApplication::event(ev);
        return cursor;
    };

    void pipeclose() {
        osApplication->quit();
    };
    QLogger::QLoggerManager *manager;
    QSocketNotifier* notifier;
};

int main(int argc, char *argv[]) {
    /// qInstallMessageHandler(MessageRewrite);
    /// qInstallMessageHandler qt5 on only console fail 
    PipeApp app(argc, argv);
    return app.exec();
}

#include "main.moc"